'''
    该案例演示字典
'''
from logging.config import dictConfig

# 创建字典
# dict1 = {}
# dict2 = dict()
# dict3 ={"a":'A',"b":'B',"c":'C'}
# dict4 = dict(a="A",b="B",c="C")
# dict5 = dict([("a","A"),("b","B"),("c","C")])
# print(type(dict1))   #<class 'dict'>
# print(type(dict2))    #<class 'dict'>
# print(dict3)  #{'a': 'A', 'b': 'B', 'c': 'C'}
# print(dict4)  #{'a': 'A', 'b': 'B', 'c': 'C'}
# print(dict5)  #{'a': 'A', 'b': 'B', 'c': 'C'}
#
# dict6 = {i:i*2 for i in range(10) if i%2 ==0}
# print(dict6)   #{0: 0, 2: 4, 4: 8, 6: 12, 8: 16}

# 访问字典
# dict1 = {"name1":"科比","age1":24,"name2":"詹姆斯","age2":23}
# print(dict1["name1"])
# print(dict1["age1"])
# print(dict1["age2"])
# print(dict1["name2"])
# # print(dict1["age5"])  # 没有对应的key,会报错
# print(dict1.get("age2"))
# print(dict1.get("name2"))
# print(dict1.get("age1"))
# print(dict1.get("name1"))
# print(dict1.get("name10"))  # 没有对应的key,会返回None

# 向字典添加元素以及对元素进行修改
# dict1 = {"name1":"科比","age1":24,"name2":"詹姆斯","age2":23}
# dict1["age1"] = 41
# dict1["adder"] = "美国"
# print(dict1)

# 判断字段是否包含key
# dict1 = {"name1": "科比", "age1": 24, "name2": "詹姆斯", "age2": 23}
# print("name1" in  dict1)
# # 不能对value进行判断
# print("科比" in dict1)


# 获取字典的长度
# dict1 = {"name1": "科比", "age1": 24, "name2": "詹姆斯", "age2": 23}
# print(len(dict1))



# dict1 = {"swk":'孙悟空',"bj":"八戒","shs":"沙和尚","ts":"唐僧","blm":'白龙马'}
# 遍历字典所有的kye
# keys =dict1.keys()
# print(keys)
# for key in  keys:
#     print(key)
# 遍历字典所有的value
# values= dict1.values()
# print(values)
# for v in values:
#     print(v)
# 遍历k-v的键值对 --第一种方式
# keys =dict1.keys()
# print(keys)
# for key in  keys:
#     print(f"{key}--------------{dict1[key]}")

# 遍历k-v的键值对 --第二种方式
# items = dict1.items()
# print(items)
# print(type(items))
# for item in items:
#     print(item)

# 删除元素
# dict1 = {"swk": '孙悟空', "bj": "八戒", "shs": "沙和尚", "ts": "唐僧", "blm": '白龙马'}
# del dict1['ts']
# print(dict1)
# 清空字段元素
# dict1.clear()
# print(dict1)

# dict.pop(key[,default]) 获取key所对应的value，同时删除该键值对，可设置默认值
# print(dict1.pop("swk"))   #孙悟空
# print(dict1.pop("齐天大圣","aaaa"))   #aaaa
# print(dict1)   #{'bj': '八戒', 'shs': '沙和尚', 'ts': '唐僧', 'blm': '白龙马'}

# dict.popitem()  取出字典中的最后插入的键值对，字典为空则报错
# print(dict1.popitem())  # ('blm', '白龙马')
# print(dict1)  #{'swk': '孙悟空', 'bj': '八戒', 'shs': '沙和尚', 'ts': '唐僧'}

# dict1.update(dict2) 将dict2中的键值对更新到dict1中
# dict1 = {"name1":"哪吒"}
# dict2 = {"name2":"封神榜"}
# dict1.update(dict2)
# print(dict1)   #{'name1': '哪吒', 'name2': '封神榜'}

# dict.setdefault(key[,default]) 获取字典中key对应value，可设置默认值。若key不存在于字典中，将会添加key并将value设为默认值
# dict2 = {"name2":"封神榜"}
# dict2.setdefault("zr","aaaa")   #default默认值如果没有,value则返回None,如果有,则返回对应的aaaa
# print(dict2)   #{'name2': '封神榜', 'zr': None}

# dict.fromkeys(seq[,default]) 以序列seq中元素做字典的key创建一个新字典，可设置value的默认值
dict2 = {"name2":"封神榜"}
dict3 = dict2.fromkeys(range(5))
print(dict3)   #{0: None, 1: None, 2: None, 3: None, 4: None}